Skip to content

fix: handle gh pr merge in worktree context#4

Merged
avifenesh merged 4 commits intomainfrom
fix/ship-3-worktree-merge
Feb 24, 2026
Merged

fix: handle gh pr merge in worktree context#4
avifenesh merged 4 commits intomainfrom
fix/ship-3-worktree-merge

Conversation

@avifenesh
Copy link
Contributor

Summary

  • Detect git worktree context in merge, cleanup, deployment, and error handling phases
  • Use gh pr merge --repo without --delete-branch when in worktree (avoids fatal: 'main' is already used by worktree)
  • Get merge SHA from PR metadata instead of local checkout to avoid race conditions
  • Block multi-branch deployment/rollback from worktrees with clear error messages pointing to main repo

Test Plan

  • Verify .git is a file in worktree (detection logic): [ -f "$(git rev-parse --show-toplevel)/.git" ]
  • Run /ship from a worktree created by /next-task - merge should complete without checkout error
  • Run /ship from a normal clone - behavior should be unchanged
  • Attempt multi-branch deployment from worktree - should get clear error with main repo path

Closes #3

When /ship runs from a git worktree, commands like `git checkout`,
`gh pr merge --delete-branch`, and `git branch -D` fail because
worktrees cannot freely switch branches. This adds worktree detection
(checking if .git is a file rather than a directory) and adapts:

- Phase 6 merge: use remote-only strategy, fetch instead of checkout
- Phase 11 cleanup: skip local branch ops in worktree mode
- Cancel/cleanup: close PR and delete remote branch separately
- Deployment (Phase 9): block with clear error, require main repo
- Rollback: block with clear error, require main repo
- Get merge SHA from PR metadata instead of fetch (avoids race condition)
- Add OWNER/REPO validation before merge command
- Move worktree checks before output in deployment/rollback
- Add fallback worktree detection in Phase 11 cleanup
- Surface branch deletion warnings instead of silent suppression
- Add 2>/dev/null to git rev-parse in error paths
- Include main repo path in deployment error messages
@avifenesh avifenesh merged commit 542c07f into main Feb 24, 2026
3 checks passed
@avifenesh avifenesh deleted the fix/ship-3-worktree-merge branch February 24, 2026 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh pr merge fails when running from a worktree

1 participant